package tech.wetech.admin3.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.wetech.admin3.sys.exception.AooUserException;
import tech.wetech.admin3.sys.service.AooPhoneCodeService;
import tech.wetech.admin3.util.CustomResponse;

@RestController
@RequestMapping("/phone")
public class AooPhoneCodeController {
  private AooPhoneCodeService phoneCodeService;

  public AooPhoneCodeController(AooPhoneCodeService phoneCodeService) {
    this.phoneCodeService = phoneCodeService;
  }

  @PostMapping("/sendcode")
  public ResponseEntity<?> sendCode(
    @RequestParam("phone") String phone
  ) {
    try {
      boolean flag = phoneCodeService.sendPhoneCode(phone);
      if (flag) {
        return ResponseEntity.ok(new CustomResponse.r1("ok", true, null));
      } else {
        return ResponseEntity.ok(new CustomResponse.r1("fail", false, null));
      }
    } catch (AooUserException e) {
      // 在这里处理异常，例如返回错误信息
      return ResponseEntity.ok(new CustomResponse.r1(e.getMessage(), false, null));
    }
  }

  @PostMapping("/verifyCode")
  public ResponseEntity<?> verifyCode(@RequestParam String phone, @RequestParam String code) {
    boolean flag = phoneCodeService.verifyCode(phone, code);
    if (flag) {
      return ResponseEntity.ok(new CustomResponse.r1("验证码正确", true, ""));
    } else {
      return ResponseEntity.ok(new CustomResponse.r1("验证码错误或已过期", false, ""));
    }
  }

}
